package com.jk.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jk.controller.BaseController;
import com.jk.pojo.Recruit;
import com.jk.pojo.RecruitDelivery;
import com.jk.service.RecruitDeliveryService;
import com.jk.service.RecruitService;

/**
 * 后台招聘模块
 * @author cuiP
 *
 */
@Controller
@RequestMapping("/admin/recruit/")
public class RecruitController extends BaseController {

	private static final String JSP_PATH = "/recruit/";
	
	@Resource
	private RecruitService recruitService;
	@Resource
	private RecruitDeliveryService deliverHistoryService;
	
	/**
	 * 招聘列表
	 * @param request
	 * @param modelMap
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("list{path}.do")
	public String list(HttpServletRequest request,@PathVariable("path") String path,ModelMap modelMap) throws Exception{
		String name = request.getParameter("name");
		String industryId = request.getParameter("industryId");
		String firstTimeStr = request.getParameter("firstTime");
		String endTimeStr = request.getParameter("endTime");
	
		//查询
		List<Recruit> recruitList = recruitService.queryList(name, firstTimeStr, endTimeStr,industryId,path);
		
		//回显
		modelMap.put("list", recruitList);
		modelMap.put("path", path);
		modelMap.put("name", name);
		modelMap.put("firstTime", firstTimeStr);
		modelMap.put("endTime", endTimeStr);
		modelMap.put("industryId", industryId);
		return JSP_PATH+"list"+path;
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("add{path}.do")
	public String add(HttpServletRequest request,@PathVariable("path") String path,ModelMap modelMap){
		modelMap.put("path", path);
		return JSP_PATH+"add";
	}
	
	/**
	 * 保存
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="save{path}.do",method=RequestMethod.POST)
	public Map<String, Object> save(HttpServletRequest request,@PathVariable("path") String path,Recruit recruit){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			recruit.setIsSend(true);
			recruit.setSendTime(new Date());
			recruit.setToNumber(0);
			if("_job".contains(path)){
				recruit.setRecruitType(1);
			}else if ("_industry".contains(path)) {
				recruit.setRecruitType(2);
			}
			//TODO  添加发布人
			recruitService.save(recruit);
			
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_SAVE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","保存失败!");
		}
		return messageMap;
	}
	
	/**
	 * 跳转到修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping("edit{path}.do")
	public String edit(HttpServletRequest request,@RequestParam(required=true,defaultValue="0") Long id,@PathVariable("path") String path,ModelMap modelMap){
		Recruit recruit = recruitService.queryById(id);
		modelMap.put("recruit", recruit);
		modelMap.put("path", path);
		return JSP_PATH+"edit";
	}
	
	/**
	 * 更新
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update.do",method=RequestMethod.POST)
	public Map<String, Object> update(HttpServletRequest request,Recruit recruit){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			//TODO  添加发布人
			recruitService.updateSelective(recruit);
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_UPDATE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","更新失败!");
		}
		return messageMap;
	}
	
	
	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delete/{id}.do",method=RequestMethod.DELETE)
	public Map<String, Object> delete(HttpServletRequest request,@PathVariable("id") Long id){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			Recruit record = recruitService.queryById(id);
			if(record == null){
				messageMap.put("status", FAILURE);
				messageMap.put("message","记录不存在!");
				return messageMap;
			}
			
			recruitService.deleteById(id);
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_DELETE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","删除失败!");
		}
		return messageMap;
	}
	
	/**
	 * 查看详情页面
	 * @param request
	 * @return
	 */
	@RequestMapping("view{path}.do")
	public String view(HttpServletRequest request,@RequestParam(required=true,defaultValue="0") Long id,@PathVariable("path") String path,ModelMap modelMap){
		Recruit recruit = recruitService.queryById(id);
		modelMap.put("recruit", recruit);
		modelMap.put("path", path);
		return JSP_PATH+"view";
	}
	
	/**
	 * 招聘投递记录列表
	 * @param request
	 * @return
	 */
	@RequestMapping("list_recruit_history.do")
	public String list(HttpServletRequest request, Long recruitId,ModelMap modelMap){
		List<RecruitDelivery> userlist = deliverHistoryService.findRecruitHistorylist(recruitId);
		modelMap.put("list", userlist);
		modelMap.put("recruitId", recruitId);
		return JSP_PATH+"list_recruit_history";
	}
}
